package com.example.memoapp.activities;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.example.memoapp.R;
import com.example.memoapp.database.MemoDatabaseHelper;
import com.example.memoapp.fragments.MemoListFragment;
import com.example.memoapp.models.Post;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MemoDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        dbHelper = new MemoDatabaseHelper(this);

        if (savedInstanceState == null) {
            MemoListFragment fragment = MemoListFragment.newInstance();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, fragment);
            transaction.commit();
        }//初始化时加载MemoListFragment
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_add) {
            startActivity(new Intent(this, AddMemoActivity.class));
            return true;
        } else if (id == R.id.action_settings) {
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        } else if (id == R.id.action_logout) {
            getSharedPreferences("loginPrefs", MODE_PRIVATE).edit().clear().apply();
            startActivity(new Intent(this, LoginActivity.class));
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public List<Post> getAllPosts() {
        List<Post> posts = new ArrayList<>();
        Cursor cursor = dbHelper.getAllMemos();

        if (cursor != null && cursor.moveToFirst()) {
            do {
                Post post = new Post();
                post.setId(cursor.getInt(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_ID)));
                post.setTitle(cursor.getString(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_TITLE)));
                post.setContent(cursor.getString(cursor.getColumnIndex(MemoDatabaseHelper.COLUMN_CONTENT)));
                posts.add(post);
            } while (cursor.moveToNext());
            cursor.close();
        }
        return posts;
    }

    // 使用 Activity 打开详情页
    public void openPostDetail(int postId) {
        Intent intent = new Intent(this, MemoDetailActivity.class);
        intent.putExtra("memoId", postId);
        startActivity(intent);
    }

    // 保留 Fragment 切换方法，用于其他场景
    private void loadFragment(Fragment fragment, boolean addToBackStack) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }
}